package edu.pku.sei.modeler.uml.commands.timing;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;

import edu.pku.sei.modeler.exceptions.FactoryNotFoundException;
import edu.pku.sei.modeler.model.common.AbstractFactory;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.StructureFactory;
import edu.pku.sei.modeler.model.uml.temp.timeline.TimingLifeLine;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommand;
import edu.pku.sei.modeler.uml.commands.UMLElementCreateCommandFactory;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

public class TimingDiagramElementCreateCommandFactory extends
		UMLElementCreateCommandFactory {
	public static TimingDiagramElementCreateCommandFactory INSTANCE = new TimingDiagramElementCreateCommandFactory();
	@Override
	public Command createCommand(MetaNode node, MetaDiagram diagram,
			Rectangle constraint) {
		if (node.getModelElement() instanceof TimingLifeLine) {
			CompoundCommand cmd = new CompoundCommand();
			cmd.add(new TimingLifeLineCreateCommand(node, diagram, constraint));
			
			AbstractFactory f = null;
			StructureFactory sf = null;
			try {
				f = ModelRegistry.getRegistry().getFactory(node.getModel());
				sf = (StructureFactory) ModelRegistry.getRegistry().getStructureFactory(node.getModel());
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FactoryNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			MetaLink link = sf.createMetaLink();
			link.setModelElement((MetaModelElement) f.createElement(ModelPackage.__TIMELINE__, false));
			UMLConnectionCreateCommand linecmd = new TimeLineCreateCommand(link, node);
			linecmd.setTarget(node);
			cmd.add(linecmd);
			
			return cmd;
		}
		return super.createCommand(node, diagram, constraint);
	}
	@Override
	public Command createCommand(MetaNode node, MetaNode hostNode,
			Rectangle constraint) {
		// TODO Auto-generated method stub
		return super.createCommand(node, hostNode, constraint);
	}
}
